$(function(){
    var page=1;
    var pagenum=10;
    var fi =1;
	var tddata=[
		{
			key:'name',
			type:1,
			url:''
		},{
			key:'id_card',
			type:1,
			url:'{:url("detail")}',
			id:137
		},{
			key:'phone',
			type:1,
			url:''
		},{
			key:'age',
			type:1,
			url:''
		},{
			key:'language',
			type:1,
			url:''
		},{
			key:'original_position',
			type:1,
			url:''
		},{
			key:'sex2',
			type:1,
			url:''
		},{
			key:'company_name',
			type:1,
			url:''
		},{
			key:'birthday',
			type:1,
			url:''
		},
		{
			key:'driving_age',
			type:1,
			url:''
		},
		{
			key:'bank',
			type:1,
			url:''
		},{
			key:'bank_card',
			type:1,
			url:''
		},
		{
			key:'wechat',
			type:1,
			url:''
		},{
			key:'alipay',
			type:1,
			url:''
		},
		{
			key:'driver_no',
			type:1,
			url:''
		},{
			key:'add',
			type:1,
			url:''
		}];
	
	var addoperation=``;
	var fields='';
	for (var fieidx=0;fieidx<tddata.length;fieidx++){
		if(fieidx==0){
			if(tddata[fieidx].key=='sex2'){
				fields+='sex'
			}else{
				fields+=tddata[fieidx].key
			}
			
		}else{
			if(tddata[fieidx].key=='sex2'){
				fields+=',sex'
			}else{
				fields+=','+tddata[fieidx].key
			}
			
		}
	}
	var data={
		page:1,
		pagenum:10,
		fields:fields
	}
	
	var where_json={}
	{if $Think.get.name}
		where_json.name=['like','%{$Think.get.name}%']
	{/if}
	{if $Think.get.phone}
		where_json.phone=['like','%{$Think.get.phone}%']
	{/if}
	{if $Think.get.company_name}
		where_json.company_name=['like','%{$Think.get.company_name}%']
	{/if}
	{if $Think.get.number}
		where_json.number=['like','%{$Think.get.number}%']
	{/if}

	data.where_json=JSON.stringify(where_json)
	ajaxdriver(data)
	function ajaxdriver(data){
		Load()
		$.ajax({
			type:'post',
			data:data,
			url:api.driver.list,
			success:function(res){
				Load(1)
				console.log(res)
				if(res.code==1){
				for(var i=0;i<res.data.length;i++){
					if(res.data[i].sex==1){
						res.data[i].sex2='男'
					}else{
						res.data[i].sex2='女'
					}
				}
			
				 //展示已知数据
				 	 table.render({
				    	elem: '#driver'
					    ,cols: [[ //标题栏
					      {field: 'name', title: '姓名', width: 150, sort: true,fixed: 'left'}
					      ,{field: 'id_card', title: '身份证号', width: 180,fixed: 'left'}
					      ,{field: 'phone', title: '手机号', minWidth: 150}
					      ,{field: 'age', title: '年龄', minWidth: 150}
					      ,{field: 'language', title: '掌握语言', width: 180}
					      ,{field: 'original_position', title: '原职位', minWidth: 150}
					      ,{field: 'sex2', title: '性别', minWidth: 150}
					      ,{field: 'company_name', title: '所属公司', width: 150}
					      ,{field: 'birthday', title: '出生年月', minWidth: 150}
					      ,{field: 'driving_age', title: '驾龄', minWidth: 150}
					      ,{field: 'bank', title: '银行名称', width: 150}
					      ,{field: 'bank_card', title: '银行账号', minWidth: 150}
					      ,{field: 'wechat', title: '微信账号', minWidth: 150}
					      ,{field: 'alipay', title: '支付宝账号', width: 150}
					      ,{field: 'driver_no', title: '司机工号', minWidth: 150}
					      ,{field: 'add', title: '居住地', minWidth: 150}
					      
					    ]]
					    ,data:res.data
						,even: true
						,limit: data.pagenum
                      });
                    //   分页
                      if (fi == 1) {
                        laypage.render({
                            elem: 'page',
                            count: res.count,
							layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
							theme: '#1E9FFF',
                            jump: function (obj, first) {
                                console.log(obj)
                                if (!first) {
                                    page = obj.curr;
                                    data.page = page;
                                    pagenum = obj.limit;
                                    data.pagenum = pagenum;
                                    ajaxdriver(data);
                                } else {
            
                                }
                            }
						})
						$('.daochu').on('click',function(){
							var getkey="";
							for(var iii=0;iii<tddata.length;iii++){
								if(tddata[iii].key=='sex2'){
									for(var key in res.description){
										if(key=='sex'){
											tddata[iii].text=res.description[key].name
										}
									}
									getkey+=`<li>
											<label class="am-checkbox">
											  <input type="checkbox" value="" data-key='sex' checked> ${tddata[iii].text}
											</label>
										</li>`
								}else{
									for(var key in res.description){
										if(key==tddata[iii].key){
											tddata[iii].text=res.description[key].name
										}
									}
									getkey+=`<li>
											<label class="am-checkbox">
											  <input type="checkbox" value="" data-key='${tddata[iii].key}' checked> ${tddata[iii].text}
											</label>
										</li>`
								}
								
							}
							layer.open({
								  type: 1,
								  title:false,
								  skin: 'layui-layer-rim', //加上边框
								  area: ['300px', '400px'], //宽高
								  content: `<div>
											 <div style='height:300px;overflow:auto;margin-bottom:30px'>
											  <ul class='daochuul' style="padding:10px;">${getkey}</ul>
											  </div>
											  <div style='text-align:center'>
												  <button class="layui-btn layui-btn-normal daochu_ok" type='button'>确定导出</button>
												  <button class='layui-btn layui-btn-danger am-radius' type='button'>取消导出</button>
											  </div>
										</div>`,
								success:function(){
									var ddata={}
									$('.daochu_ok').on('click',function(){
										ddata.fields="";
										var where_jsondao={};
										{if $Think.get.name}
											where_jsondao.name=['like','%25{$Think.get.name}%25']
										{/if}
										{if $Think.get.phone}
											where_jsondao.phone=['like','%25{$Think.get.phone}%25']
										{/if}
										{if $Think.get.company_name}
											where_jsondao.company_name=['like','%25{$Think.get.company_name}%25']
										{/if}
										{if $Think.get.number}
											where_jsondao.number=['like','%25{$Think.get.number}%25']
										{/if}
										
										ddata.where_json=JSON.stringify(where_jsondao)
										
										$('.daochuul li input[type="checkbox"]').each(function(){
											if(this.checked){
												ddata.fields+=($(this).data('key')+",")
											}
										})
										ddata.fields=ddata.fields.slice(0,ddata.fields.length-1)
										console.log(ddata.fields)
										console.log(`{:url("adminapi/Driver/downLoadDriverList")}?where_json=${ddata.where_json}&fields=${ddata.fields}`)
										window.open(`${api.driver.down}?where_json=${ddata.where_json}&fields=${ddata.fields}`)							
									})
									
								}
							});
						})
                        fi = 2;
                    }

				}else{
					Err(res.msg)
				}
			},
			error:function(){
				Err('请求失败')
			}
		})
	}
})
